package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.RoleDao;
import cn.edu.sdjzu.xg.bysj.domain.authority.Role;
import exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Collection;
import java.util.List;

public final class RoleService {
    private static final RoleDao roleDao = RoleDao.getInstance();
    private static final RoleService roleService = new RoleService();

    public static RoleService getInstance() {
        return roleService;
    }

    public Collection<Role> findAll(Pagination pagination,
                                    List<Condition> conditionList)
            throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        Collection<Role> roles = roleDao.findAll(connection, pagination, conditionList);
        //释放连接
        JdbcHelper.close(connection);
        return roles;
    }

    public Role find(Integer id) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        Role role = roleDao.find(connection, id);
        //释放连接
        JdbcHelper.close(connection);
        return role;
    }

    public boolean update(Role role) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean updated = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            updated = roleDao.update(connection, role);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(Role role) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean added = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            added = roleDao.add(connection, role);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return added;
    }

    public boolean delete(Integer id) throws SQLException, BysjException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean deleted = false;
        try {
            deleted = roleDao.delete(connection, id);
            return deleted;
        } catch (SQLIntegrityConstraintViolationException e) {
            throw new BysjException("某员工或学生正在使用当前角色");
        } finally {
            //释放连接
            JdbcHelper.close(connection);
        }

    }

}
